

import 'package:basic_common/common_exports.dart';
import 'package:basic_common/global_getx.dart';
import 'package:basic_modle/dr_profile_data.dart';
import 'package:basic_modle/duty_data.dart';
import 'package:basic_modle/upload_data.dart';
import 'package:basic_widgets/bottom_img_picker.dart';

class PersonalController extends GetxController {
  // 团队基本信息
  final basicInfoList = [
    {"attr": "头像", "value": "", "isEdit": true},
    {"attr": "真实姓名", "value": "李毅", "isEdit": true},
    {"attr": "编号", "value": "300050", "isEdit": false},
    {"attr": "联系电话", "value": "18790252645", "isEdit": false},
    {"attr": "团队职位", "value": "全科医师", "isEdit": true},
    {"attr": "医生简介", "value": "从事内分泌相关疾病35余年", "isEdit": true},
    {"attr": "擅长", "value": "阜外中心医院", "isEdit": true},
  ].obs;

  /// 头像地址
  final headImgUrl = ''.obs;
  final docdata = DoctorData().obs;

  TextEditingController nickInputController = TextEditingController();
  TextEditingController profileController = TextEditingController();
  TextEditingController goodController = TextEditingController();

  final doctorInfo = DoctorData().obs;
  final identityInfo = IdentityData().obs;

  final userId = ''.obs; //用户Id
  final doctorNo = ''.obs; //
  final phone = ''.obs; //手机号

  final dutyList = <String>[].obs;
  final allDutyList = <DutyData>[].obs;
  final dutyIdList = <String>[].obs;

  final selectDuty = '请选择您的职务'.obs;
  final selectDutyId = '-100'.obs; //选择的职务ID

  final docProfile = ''.obs; //个人简介
  final docGoodAt = ''.obs;
  FocusNode namefocusNode = FocusNode();
  FocusNode profilefocusNode = FocusNode();
  FocusNode goodAtfocusNode = FocusNode();

  @override
  void onInit() {
    super.onInit();
    getDrProfile();
  }

  /// 获取医生信息
  getDrProfile() async {
    HttpData res = await ApiService().drProfile(data: {
      "scene": "get-by-token",
      "sceneDesc": "",
      "searchKey": "Token",
      "searchValue": ""
    });
    if (res.code == 1) {
      DrProfileData drData = DrProfileData.fromJson(res.data);
      doctorInfo.value = drData.doctor!;
      identityInfo.value = drData.identity!;
      userId.value = doctorInfo.value!.userId ?? '';
      doctorNo.value = doctorInfo!.value.doctorNo ?? '';
      headImgUrl.value =
          doctorInfo!.value != null && doctorInfo!.value.profilePhoto != null
              ? doctorInfo!.value.profilePhoto!
              : "";
      nickInputController.text = doctorInfo!.value.doctorNick ?? '';
      doctorNo.value = doctorInfo!.value.doctorNo ?? '';
      phone.value = doctorInfo!.value.phone ?? "";
      selectDuty.value = doctorInfo!.value.dutyName ?? "";
      selectDutyId.value = doctorInfo!.value.duty.toString() ?? "";
      docProfile.value = doctorInfo!.value.personalProfile ?? '';
      profileController.text = doctorInfo!.value.personalProfile ?? '';
      docGoodAt.value = doctorInfo!.value.excelAt.toString() ?? '';
      goodController.text = doctorInfo!.value.excelAt.toString() ?? '';
      update(['personalInfo']);
    }
  }

  /// 选择头像
  void showSelectImg() {
    BottomImagePickerTool.showBottomSheet(
        onCamera: () => getCameraImgs(), onGallery: () => getImgs());
  }

  /// 相机拍照
  getCameraImgs() async {
    XFile? _image;
    final picker = ImagePicker();
    _image = await picker.pickImage(source: ImageSource.camera);
    headImgUrl.value = await uploadSingleFile(_image!.path);
    updateDrInfo();
    update(['personalInfo']);
  }

  /// 选择相册图片
  getImgs() async {
    XFile? _image;
    final picker = ImagePicker();
    _image = await picker.pickImage(source: ImageSource.gallery);
    headImgUrl.value = await uploadSingleFile(_image!.path);
    updateDrInfo();
    update(['personalInfo']);
  }

  /// 单个图片上传
  Future<String> uploadSingleFile(String path) async {
    String userId = globalGetx.doctorInfo!.userId!;
    UploadData res = await ApiService().uploadSingle(params: {
      "file": await MultipartFile.fromFile(path),
      "domain": 'fw/${userId}'
    });
    if (res.errCode == 0) {
      return res.uri!;
    }
    return '';
  }

  /// 更新医生信息
  void updateDrInfo() async {
    HttpData res = await ApiService().updateDrInfo(data: {
      "doctorNo": doctorNo.value,
      "profilePhoto": headImgUrl.value,
      "personalProfile": profileController.text,
      "beGoodAt": goodController.text,
      "duty": selectDutyId.value,
      "doctorNick": nickInputController.text
    });
    if (res.code == 1) {
      ToastUtils.toast("修改成功");
      globalGetx.getDrProfile();
      Get.back();
    } else {
      ToastUtils.toast("修改成功");
    }
  }

  /// 获取职称
  /// 职务、职称 type doctor_duty   职务 doctor_title
  getDutyList({required String type}) async {
    if (type == "doctor_duty") {
      allDutyList.clear();
      dutyList.clear();
      dutyIdList.clear();
    }
    HttpData httpdata =
        await ApiService().getDutyList(data: {"dictType": type});
    if (httpdata.code == 1) {
      List<dynamic> list = httpdata.data;
      for (dynamic itemData in list) {
        DutyData dutyData = DutyData.fromJson(itemData);
        if (type == "doctor_duty") {
          //职务
          allDutyList.add(dutyData);
          dutyList.add(dutyData.dictLabel!);
          dutyIdList.add(dutyData.dictValue!);
        }
      }
      update(['dutyList']);
    }
  }
}
